import { sendertList, senderOperate, senderDetails, ableSenderOperate } from '@/services/manageMsg';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'sender',

  state: {
    sendertList: {},
    senderDetails: {},
    dictDataDetails: {},
  },

  effects: {
    *sendertList({ payload, callback }, { call, put }) {
      const response = yield call(sendertList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'show',
          payload: {
            sendertList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *senderOperate({ payload, callback }, { call }) {
      const response = yield call(senderOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *senderDetails({ payload, callback }, { call, put }) {
      const response = yield call(senderDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            senderDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *ableSenderOperate({ payload, callback }, { call }) {
      const response = yield call(ableSenderOperate, payload);
      if (response.code === 0) {
        Prompt({ content: response.message });
        callback(response);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        sendertList: payload.sendertList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        senderDetails: { ...state.senderDetails, ...payload.senderDetails },
      };
    },
  },
};
